package com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.DO;

import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.SentinelAuthorityRuleDTO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serializable;

/**
 * xx
 *
 * @author BugFree Jin
 * @version 1.0
 * @since 2022/3/2 15:44
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Table(value = "sentinel_authority_rule")
public class SentinelAuthorityRuleDO extends BaseDO {
    private static final long serialVersionUID = -8285934275224360856L;
    @Id
    private long id;

    @Column("authority_rule")
    private String authorityRule;

    //@Transient
    //private SentinelAuthorityRuleDTO sentinelAuthorityRuleDTO;

    public SentinelAuthorityRuleDTO getSentinelAuthorityRuleDTO() {

        SentinelAuthorityRuleDTO authorityRuleDTO = new SentinelAuthorityRuleDTO();
        try {
            AuthorityRule ar = new ObjectMapper().readValue(this.authorityRule, AuthorityRule.class);
            BeanUtils.copyProperties(this,authorityRuleDTO);
            BeanUtils.copyProperties(ar, authorityRuleDTO, "authorityRule");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return authorityRuleDTO;
    }

    @Data
    public static class AuthorityRule implements Serializable {
        private static final long serialVersionUID = -8527927049808688381L;
        private String limitApp;
        private String resource;
        private Integer strategy;
    }
}
